package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.cvo.DeliveryDetailVo;
import com.woniuxy.oldmanapi.entity.DeliveryMedicineBill;
import com.woniuxy.oldmanapi.service.DeliveryDetailService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class DeliveryDetailController {
    @Autowired
    DeliveryDetailService deliveryDetailService;

    //出库账单查询
    @GetMapping("/deliveryBill/search")
    public ResponseResult<PageInfo<DeliveryDetailVo>> list(@RequestParam(value = "beginTime", defaultValue = "null")Date beginTime,
                                                           @RequestParam(value = "endTime", defaultValue = "null") Date endTime,
                                                           @RequestParam(value = "olderId", defaultValue = "0") int olderId,
                                                           @RequestParam(value = "size", defaultValue = "6") int size,
                                                           @RequestParam(value = "now", defaultValue = "1") int now) {


        ResponseResult<PageInfo<DeliveryDetailVo>> result=new ResponseResult<>();
        Date begin=null;
        Date end=null;
        if(beginTime==null){
            beginTime=new Date(0);
        }
        if(endTime==null){
            endTime=new Date();
        }
        try {
            if(beginTime.getTime()>=endTime.getTime()){
                begin=endTime;
                end=beginTime;
            }else {
                begin=beginTime;
                end=endTime;
            }
            if(begin.getTime()==end.getTime()){
                end=new Date(end.getTime()+24*60*60*1000);
            }
            PageInfo<DeliveryDetailVo> pageInfo=deliveryDetailService.findByCons(begin,end,olderId,size,now);
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("查询失败");
            result.setCode(201);
        }
        return result;
    }
}
